package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.result.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.ShortMessageLoginParam;
import com.example.lotterysystem.controller.param.UserPasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.UserBaseInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.VerificationCodeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;
    /**
     * 用户注册接口
     * @param param
     * @return
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> register(
            @Validated @RequestBody UserRegisterParam param) {
        // 日志打印
        log.info("UserController register 接收到参数: {}",
                JacksonUtil.writeValueAsString(param));

        // 业务处理
        UserRegisterDTO userRegisterDTO = userService.register(param);

        // 返回结果
        return CommonResult.success(convertToRegisterResult(userRegisterDTO));
    }

    /**
     * 结果类型转化
     * 将 UserRegisterDTO 类型转化为 UserRegisterResult 类型
     * @param userRegisterDTO
     * @return
     */
    private UserRegisterResult convertToRegisterResult(UserRegisterDTO userRegisterDTO) {
        if (null == userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        UserRegisterResult userRegisterResult = new UserRegisterResult();
        userRegisterResult.setUserId(userRegisterDTO.getUserId());
        return userRegisterResult;
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber) {
        log.info("UserController sendVerificationCode 接收到参数 phoneNumber: {}", phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> loginByPassword(
            @Validated @RequestBody UserPasswordLoginParam param) {
        // 日志打印
        log.info("UserController loginByPassword 接收到参数 UserPasswordLoginParam: {}",
                JacksonUtil.writeValueAsString(param));
        // 业务处理
        UserLoginDTO userLoginDTO = userService.login(param);

        // 业务是否处理成功
        if (null == userLoginDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        // 构造结果并返回
        return CommonResult.success(convertToLoginResult(userLoginDTO));
    }

    /**
     * 验证码登录
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> loginByShortMessage(
            @Validated @RequestBody ShortMessageLoginParam param) {
        // 日志打印
        log.info("UserController loginByShortMessage 接收到参数 ShortMessageLoginParam: {}",
                JacksonUtil.writeValueAsString(param));
        // 业务处理
        UserLoginDTO userLoginDTO = userService.login(param);
        // 业务是否处理成功
        if (null == userLoginDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        // 构造结果并返回
        return CommonResult.success(convertToLoginResult(userLoginDTO));
    }

    /**
     * 结果类型转化
     * 将 UserLoginDTO 类型转化为 UserLoginResult
     * @param userLoginDTO
     * @return
     */
    private UserLoginResult convertToLoginResult(UserLoginDTO userLoginDTO) {
        if (null == userLoginDTO) {
            return null;
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentify(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    /**
     * 获取人员管理 - 人员列表
     * @param identity 要查询用户的身份信息
     * @return
     */
    @RequestMapping("/find-list")
    public CommonResult<List<UserBaseInfoResult>> findUserBaseInfoList(String identity) {
        // 日志打印
        log.info("UserController findUserBaseInfoList 接收到参数identity : {}",
                JacksonUtil.writeValueAsString(identity));
        // 进行业务处理
        List<UserDTO> userDTO = userService.findUserBaseInfoList(identity);
        // 构造响应并返回
        return CommonResult.success(
                convertToUserBaseInfoResultList(userDTO));
    }

    /**
     * 将列表中的 UserDTO 转化为 UserBaseInfoResult
     * @param userDTOList
     * @return
     */
    private List<UserBaseInfoResult> convertToUserBaseInfoResultList(List<UserDTO> userDTOList) {
        // 判断列表是否为空
        if (CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }
        return userDTOList.stream()
                .map(userDTO -> {
                    UserBaseInfoResult userBaseInfoResult = new UserBaseInfoResult();
                    userBaseInfoResult.setUserId(userDTO.getUserId());
                    userBaseInfoResult.setUserName(userDTO.getUserName());
                    userBaseInfoResult.setIdentity(userDTO.getIdentity().name());
                    return userBaseInfoResult;
                }).collect(Collectors.toList());
    }
}
